//
//  Int+Extension.swift
//  LiveTrivia
//
//  Created by Gesen on 2017/11/12.
//  Copyright © 2017年 LiveTrivia. All rights reserved.
//

import UIKit

extension Int {
    
    /// 分 转换货币字符串
    ///
    /// - Parameters:
    ///   - symbol: 货币符号 默认¥
    ///   - point: 最小小数位数 默认0
    /// - Returns: 货币格式字符串
    func cents(_ symbol: String = "¥", point: Int = 0) -> String {
        return (Double(self) / 100).currency(symbol, point: point)
    }
    
    /// 分 转换货币字符串
    ///
    /// - Parameters:
    ///   - symbol: 货币符号 默认¥
    ///   - point: 最小小数位数 默认0
    ///   - rule: rounded规则
    /// - Returns: 货币格式字符串
    func cents(_ symbol: String = "¥",
               point: Int = 0,
               rule: FloatingPointRoundingRule
               ) -> String {
        return (Double(self) / 100).rounded(rule).currency(symbol, point: point)
    }
    
    /// 分 转 奖金字符串
    var prizeString: String {
        let format = NumberFormatter()
        format.numberStyle = .decimal
        format.locale = Locale(identifier: "zh-Hans_CN")
        let string = format.string(from: NSNumber(value: Double(self) / 100))
        return "¥\(string ?? "0")"
    }
    /// 分 转 价格字符串
    var priceString: String {
        let format = NumberFormatter()
        format.numberStyle = .currency
        format.locale = Locale(identifier: "zh-Hans_CN")
        let string = format.string(from: NSNumber(value: Double(self) / 100))
        return "\(string ?? "0")"
    }
    
    /// 分 转 XX.XX万字符串
    var wanString: String {
        return Double(self).wanString
    }
    
    /// 在线人数 转 XX.XX万
    var onlineString: String {
        let count = self
        if count > 9999 {
            let w = count / 10000
            let q = (count - w * 10000) / 1000
            return q == 0 ? "\(w)万" : "\(w).\(q)万"
        } else {
            return "\(count)"
        }
    }
}

extension Int {
    var bool: Bool {
        return self > 0
    }
}
